{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cdb3f824",
   "metadata": {},
   "source": [
    "# The `panelInset` Parameter in `theme()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c9fd311c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"a6wNcF\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.3.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"a6wNcF\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"a6wNcF\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b8c8734b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.0.0.0-SNAPSHOT. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "21620f83",
   "metadata": {},
   "outputs": [],
   "source": [
    "val hours = (0 until 12).map { String.format(\"%02d:00\", it) }\n",
    "\n",
    "val p = letsPlot() +\n",
    "    geomPoint() { x = hours } +\n",
    "    themeGrey() +\n",
    "    theme(axisLine = elementLine(color = \"black\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd6896f8",
   "metadata": {},
   "source": [
    "## Polar Coordinate System\n",
    "\n",
    "In the polar coordinate system, it's common for tick labels to overlap.\n",
    "\n",
    "The `panelInset` parameter assists in creating additional space to accommodate tick labels more effectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7e0dd65f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"2Ov35i\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":3,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"polar\"\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "\"x\":[\"00:00\",\"01:00\",\"02:00\",\"03:00\",\"04:00\",\"05:00\",\"06:00\",\"07:00\",\"08:00\",\"09:00\",\"10:00\",\"11:00\"]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"grey\",\n",
       "\"axis_line\":{\n",
       "\"color\":\"black\",\n",
       "\"blank\":false\n",
       "}\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"polar\"\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "\"x\":[\"00:00\",\"01:00\",\"02:00\",\"03:00\",\"04:00\",\"05:00\",\"06:00\",\"07:00\",\"08:00\",\"09:00\",\"10:00\",\"11:00\"]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"grey\",\n",
       "\"axis_line\":{\n",
       "\"color\":\"black\",\n",
       "\"blank\":false\n",
       "},\n",
       "\"panel_inset\":[0.0,35.0]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"polar\",\n",
       "\"transform_bkgr\":false\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "\"x\":[\"00:00\",\"01:00\",\"02:00\",\"03:00\",\"04:00\",\"05:00\",\"06:00\",\"07:00\",\"08:00\",\"09:00\",\"10:00\",\"11:00\"]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"grey\",\n",
       "\"axis_line\":{\n",
       "\"color\":\"black\",\n",
       "\"blank\":false\n",
       "},\n",
       "\"panel_inset\":[0.0,35.0]\n",
       "}\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"2Ov35i\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(\n",
    "    listOf(\n",
    "        p + coordPolar(),\n",
    "        p + theme(panelInset = listOf(0, 35)) + coordPolar(),\n",
    "        p + theme(panelInset = listOf(0, 35)) + coordPolar(transformBkgr = false),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e2bfc6f",
   "metadata": {},
   "source": [
    "## Rectangular Coordinate System  \n",
    "In rectangular coordinate systems, the `panelInset` parameter functions as a margin, adjusting the panel boundaries within the plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "32dac6c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"bNCtsw\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "\"x\":[\"00:00\",\"01:00\",\"02:00\",\"03:00\",\"04:00\",\"05:00\",\"06:00\",\"07:00\",\"08:00\",\"09:00\",\"10:00\",\"11:00\"]\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "},\n",
       "\"theme\":{\n",
       "\"name\":\"grey\",\n",
       "\"axis_line\":{\n",
       "\"color\":\"black\",\n",
       "\"blank\":false\n",
       "}\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "},{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "\"x\":[\"00:00\",\"01:00\",\"02:00\",\"03:00\",\"04:00\",\"05:00\",\"06:00\",\"07:00\",\"08:00\",\"09:00\",\"10:00\",\"11:00\"]\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "},\n",
       "\"theme\":{\n",
       "\"name\":\"grey\",\n",
       "\"axis_line\":{\n",
       "\"color\":\"black\",\n",
       "\"blank\":false\n",
       "},\n",
       "\"panel_inset\":15.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"bNCtsw\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(\n",
    "    listOf(\n",
    "        p,\n",
    "        p + theme(panelInset = 15)\n",
    "    )\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.8.0-dev-3517"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
